package com.awspaass.user.apps.restaurant;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.commons.formfile.model.delegate.FormFile;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
@Controller
public class AppCmdGiveLike {

	@Mapping("com.awspaass.user.apps.dinninghall_getAllRecommendation")
	public static String getAllRecommendation(UserContext uc) {

		JSONObject result = new JSONObject();
		try {
			/*
			 * Date now = new Date(); SimpleDateFormat simpleDateFormat = new
			 * SimpleDateFormat("yyyy-MM"); String yearMonth = simpleDateFormat.format(now);
			 * String year = yearMonth.substring(0, 4); String month =
			 * yearMonth.substring(5, 7); int intMonth = Integer.parseInt(month); int
			 * quarter = intMonth % 3 == 0 ? intMonth / 3 : intMonth / 3 + 1;
			 */
			String querySql = "select t.cnname ,t.itemno from  BO_ACT_DICT_KV_ITEM t where t.dictkey='shdininghallarea' and (itemno<30  or itemno>=100)";
			List<Map<String, Object>> diningList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if (diningList == null || diningList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到食堂列表信息");
				return result.toString();
			}
			JSONArray itemArray = new JSONArray();
			String today = String.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			for (int i = 0; i < diningList.size(); i++) {
				Map<String, Object> urlMap = diningList.get(i);

				String name = CoreUtil.objToStr(urlMap.get("CNNAME"));
				String itemNo = CoreUtil.objToStr(urlMap.get("ITEMNO"));
				String queryCountSql = "select count(distinct(t.caipinid)) as count, a.bindid ,a.newprdtstartdate, a.newprdtenddate from "
						+ "BO_EU_SHSYST_RECOMCAI_body t, BO_EU_SHSYST_RECOMCAI_head a "
						+ "where t.bindid = a.bindid and a.area = '" + itemNo  + "' and a.busitype = '1' and to_date(a.newprdtstartdate, 'yyyy-MM-dd') <= to_date('"
						+ today + "', 'yyyy-MM-dd') and to_date(a.newprdtenddate, 'yyyy-MM-dd') >= to_date('" + today
						+ "', 'yyyy-MM-dd') group by a.bindid, a.newprdtstartdate, a.newprdtenddate";
				//System.out.println(queryCountSql);
				List<Map<String, Object>> countList = DBSql.query(queryCountSql, new ColumnMapRowMapper(),
						new Object[] {});
				for (int j = 0; j < countList.size(); j++) {
					JSONObject item = new JSONObject();
					Map<String, Object> countMap = countList.get(j);
					String count = CoreUtil.objToStr(countMap.get("COUNT"));
					String activeId = CoreUtil.objToStr(countMap.get("BINDID"));
					String startDate = CoreUtil.objToStr(countMap.get("NEWPRDTSTARTDATE"));
					String endDate = CoreUtil.objToStr(countMap.get("NEWPRDTENDDATE"));
					item.put("badge", count);
					item.put("activeid", activeId);
					item.put("text", name);
					item.put("start", startDate);
					item.put("end", endDate);
					item.put("id", itemNo);
					itemArray.add(item);
				}

				// item.put("COUNT",count);
				// item.put()

			}
			result.put("status", "0");
			result.put("data", itemArray);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}

		return result.toString();
	}

	@Mapping("com.awspaass.user.apps.dinninghall_getRecommendation")
	public static String getRecommendation(UserContext uc, String diningId, String activeId) {
		JSONObject result = new JSONObject();
		if(null == diningId || "".equals(diningId)  ) {
			result.put("status", "1");
			result.put("message", "请检查食堂ID!");
			return result.toString();
		}
		if(null == activeId || "".equals(activeId)) {
			result.put("status", "1");
			result.put("message", "请检查活动ID!");
			return result.toString();
		}
		try {
			String queryFoodSql = "select b.id,"
					+ "a.caipinname,"
					+ "a.caipintype,"
					+ "b.caipinprice,"
					+ "b.caipinphoto,"
					+ "b.caipinresume,c.recordsum"
					+ "  from     BO_EU_SHSYST_RECOMCAI_BODY a"
					+ "  inner join  Bo_Eu_Shsyst_Recomcai_Head h on h.bindid=a.bindid"
					+ " inner join BO_EU_SHSYST_CAIDANHEAD b"
					+ "  on a.CAIPINID = b.ID"
					+ "  left join VIEW_PRDTNOTE_COUNT c on c.productid=a.caipinid and  c.notetype=h.busitype and c.activeid=a.bindid where h.busitype = '1' and  h.bindid = '"+activeId+"' and h.area = '"+diningId+"'";
			System.out.println(queryFoodSql);
			List<Map<String, Object>> foodList = DBSql.query(queryFoodSql, new ColumnMapRowMapper(), new Object[] {});
			if (foodList == null || foodList.isEmpty()) {
				result.put("status", "1");
				result.put("message", "未查询到该食堂提供的特色菜品信息.");
				return result.toString();
			}
			JSONArray foodArray = new JSONArray();
			for (int i = 0; i < foodList.size(); i++) {
				Map<String, Object> urlMap = foodList.get(i);
				JSONObject foodObj = new JSONObject();
				String foodName = CoreUtil.objToStr(urlMap.get("CAIPINNAME"));
				String foodType = CoreUtil.objToStr(urlMap.get("CAIPINTYPE"));
				String foodPrice = CoreUtil.objToStr(urlMap.get("CAIPINPRICE"));
				String foodDescribe = CoreUtil.objToStr(urlMap.get("CAIPINRESUME"));
				String food666 = CoreUtil.objToStr(urlMap.get("RECORDSUM"));
				String boId = CoreUtil.objToStr(urlMap.get("ID"));

				foodObj.put("title", foodName);
				foodObj.put("desc", foodDescribe);
				foodObj.put("price", foodPrice);
				if("".equals(food666)) {
					foodObj.put("count", 0);
				}else {
					foodObj.put("count", food666);
				}
				foodObj.put("id", boId);

				List<FormFile> files = SDK.getBOAPI().getFiles(boId, "CAIPINPHOTO");
				for (FormFile file : files) {
					String downloadUrl = SDK.getBOAPI().getFileDCContext(file).getDownloadURL();
					foodObj.put("url", downloadUrl);
				}

				foodArray.add(foodObj);
			}
			result.put("status", '0');
			result.put("data", foodArray);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}

	@Mapping("com.awspaass.user.apps.dinninghall_giveLike")
	public static String giveLike(UserContext uc, String foodId, String activeId) {

		String userId = uc.getUID();
		//String userUnit = t
		String today = String.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		JSONObject result = new JSONObject();
		if(null == foodId || "".equals(foodId)  ) {
			result.put("status", "1");
			result.put("message", "请检查食品ID!");
			return result.toString();
		}
		if(null == activeId || "".equals(activeId)) {
			result.put("status", "1");
			result.put("message", "请检查活动ID!");
			return result.toString();
		}
		try {
			String querySql = "select * from BO_EU_SHSYST_PRDTNOTE_RECORD where userId = '" + userId
					+ "' and PRODUCTID = '" + foodId + "' and ACTIVEID = '" + activeId + "' and NOTEDATE= '"
					+ today + "'";
			List<Map<String, Object>> likeList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if(likeList.size()>0) {
				result.put("status","1");
				result.put("message", "当日已经投票!");
				return result.toString();
			}
			BO boRecordData = new BO();
			boRecordData.set("NOTETYPE", "1");
			boRecordData.set("NOTEDATE", today);
			boRecordData.set("PRODUCTID", foodId);
			boRecordData.set("PRODUCTNAME", "");
			boRecordData.set("USERID", userId);
			boRecordData.set("USERUNIT", "");
			boRecordData.set("USERNAME", "");
			boRecordData.set("MEMO", "");
			boRecordData.set("ACTIVEID", activeId);
			
			
			List likeData = new ArrayList();
			likeData.add(boRecordData);
			ProcessInstance processInst = SDK.getProcessAPI().createProcessInstance("obj_05589d23ea0349f58735503d1bf99227",
					"admin", today + "投票记录");
			
			SDK.getBOAPI().create("BO_EU_SHSYST_PRDTNOTE_RECORD", likeData, processInst, UserContext.fromUID("admin"));
			result.put("status", "0");
			result.put("message", "投票成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("status", "1");
			result.put("message", e.getMessage());
		}
		return result.toString();
	}

}
